//
//  SightseeinCollection.m
//  Lovelife
//
//  Created by dllo on 15/4/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SightseeinCollection.h"
#import "RecommendTripCell.h"
#import "SceneryModel.h"
#import "UIImageView+WebCache.h"
#import "SceneryViewController.h"
@interface SightseeinCollection ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *collectionArr;
@property (nonatomic, copy) NSString *filePath;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation SightseeinCollection
- (void)dealloc
{
    [_collectionArr release];
    [_filePath release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SceneryCollectionFile.plist"];
        NSLog(@"%@",self.filePath);
        self.collectionArr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:self.filePath]];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    // 设置操作按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = @"编辑";

}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    if (editing) {
        self.navigationItem.rightBarButtonItem.title = @"完成";
    }else{
        self.navigationItem.rightBarButtonItem.title = @"编辑";
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionArr removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView reloadData];
    [NSKeyedArchiver archiveRootObject:self.collectionArr toFile:self.filePath];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectionArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    RecommendTripCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[RecommendTripCell alloc]initWithStyle:0 reuseIdentifier:reuse] autorelease];
    }
    SceneryModel *scenery = self.collectionArr[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:scenery.photo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.daysLabel.text = scenery.chinesename;
    NSLog(@"%@",scenery.chinesename);
    cell.tripLabel.text = scenery.introduction;
    cell.updateLabel.text = scenery.price;
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SceneryViewController *sceneryVC = [[SceneryViewController alloc] init];
    sceneryVC.sceneryId = [self.collectionArr[indexPath.row] sceneryId];
    [self.navigationController pushViewController:sceneryVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
